<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Post extends FrontEndController {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('repo/post_model', 'post_model');
        $this->load->library('mypaging');
    }

    private function intialPaging(){
        $this->mypaging->setCount($this->post_model->countPost());
        $this->mypaging->setLimit(POST_LIMIT);
        $this->mypaging->setMainURL(post_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function index($pageIndex=1) {
        $this->intialPaging();
        $offset    = ($pageIndex-1) * POST_LIMIT;
        $pagingBar = $this->mypaging->getBar($pageIndex);
        $aPost     = $this->post_model->pagingPost(POST_LIMIT, $offset);
        $this->load->model('repo/post_cate_model', 'post_cate_model');
        $aPostCate = $this->post_cate_model->getAllPostCate();

        $aReturn   = array(
            'pageTitle' => 'Tin tức',
            'aPost'     => $aPost,
            'pagingBar' => $pagingBar,
            'aPostCate' => $aPostCate
        );
        if($pageIndex > 1){
            $aReturn['pageTitle'] = 'Trang ' . $pageIndex . ' | Tin tức';
        }

        $this->renderView('pages/post', $aReturn);
    }

    public function get($slug) {
        $post = $this->post_model->getPost($slug);
        if(count($post)==0){
            show_404();
        }
        $nextPost= $this->post_model->getNextPost($post->getId());
        $prevPost= $this->post_model->getPrevPost($post->getId());

        $this->load->model('repo/post_cate_model', 'post_cate_model');
        $aPostCate = $this->post_cate_model->getAllPostCate();
        $aReturn = array(
            'pageTitle' => $post->getTitle(),
            'post'      => $post,
            'nextPost'  => $nextPost,
            'prevPost'  => $prevPost,
            'aPostCate' => $aPostCate
        );

        $this->renderView('pages/post-single', $aReturn);
    }

    private function intialCatePaging($slug) {
        $this->mypaging->setCount($this->post_model->countPostByCategory($slug));
        $this->mypaging->setLimit(POST_LIMIT);
        $this->mypaging->setMainURL(post_page_by_cate_url($slug));
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function getByCategory($slug, $pageIndex=1) {
		$this->intialCatePaging($slug);
        $offset             = ($pageIndex-1) * POST_LIMIT;
        $aData['pagingBar'] = $this->mypaging->getBar($pageIndex);
        $aData['aPost']     = $this->post_model->getByCategory(POST_LIMIT, $offset, $slug);
		$aData['pageTitle'] = "Tin tức";
        $this->load->model('repo/post_cate_model', 'post_cate_model');
        $aPostCate = $this->post_cate_model->getAllPostCate();
        $aData['aPostCate']     = $aPostCate;
        $aData['breadcrumCate'] = $slug;
		if($pageIndex > 1){
			$aData['breadcrumPage'] = 'Trang ' . $pageIndex;
		}
		$this->renderView('pages/post', $aData);
	}
}
?>